﻿using DevComponents.DotNetBar;
using Kimd.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;
using WorkStation.Logic;

namespace WorkStation.UI
{
    public partial class FormRobot : Office2007Form
    {
        public FormRobot()
        {
            InitializeComponent();
        }

        private void FormRobot_Load(object sender, EventArgs e)
        {
            createButton();
            createButton2();
        }
        private void createButton()
        {
            this.p_Robot.Controls.Clear();
            byte startChar = 0x41;
            int index = 0;
            foreach (var item in Enum.GetValues(typeof(RobotinEnumRobot1)))
            {
                ButtonX buttonX1 = new ButtonX();
                buttonX1 = new DevComponents.DotNetBar.ButtonX();
                //buttonX1 = new Button();
                // 
                // buttonX1
                // 
                buttonX1.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
                buttonX1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                buttonX1.BackColor = System.Drawing.SystemColors.Control;
                //buttonX1.ColorTable = DevComponents.DotNetBar.eButtonColor.Blue;
                buttonX1.Font = new System.Drawing.Font("微软雅黑", 9.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                buttonX1.Location = new System.Drawing.Point(5, 10 + 29 * index);
                buttonX1.PopupSide = DevComponents.DotNetBar.ePopupSide.Right;
                buttonX1.Shape = new DevComponents.DotNetBar.RoundRectangleShapeDescriptor(4);
                buttonX1.Size = new System.Drawing.Size(this.p_Robot.Width - 2 * 5, 26);
                //buttonX1.Style = DevComponents.DotNetBar.eDotNetBarStyle.OfficeMobile2014;
                buttonX1.TabIndex = 0;
                buttonX1.Text = EnumHelper.GetEnumDescriptionFromValue((RobotinEnumRobot1)item);
                startChar += 0x01;
                //buttonX1.Click += Module_Click;
                //buttonX1.Image = iL_Circles.Images[i];
                buttonX1.Style = eDotNetBarStyle.StyleManagerControlled;
                buttonX1.ColorTable = eButtonColor.Flat;
                buttonX1.Tag = (RobotinEnumRobot1)item;
                //if (!string.IsNullOrEmpty(ModuleNames[i]))
                //{
                //    //buttonX1.Checked = !SystemConfig.Instance.ModuleName2Disable[ModuleNames[i]];
                //    buttonX1.BackColor = GlobalVariable.RobotStatus[i] ? Color.LimeGreen : Color.Gray;
                //    buttonX1.TextColor = SystemConfig.Instance.ModuleName2Disable[ModuleNames[i]] ? Color.White : Color.Black;
                //}
                this.p_Robot.Controls.Add(buttonX1);
                if (index > 3)
                    break;
                index++;
            }
        }
        private void createButton2()
        {
            this.p_Robot2.Controls.Clear();
            byte startChar = 0x41;
            int index = 0;
            foreach (var item in Enum.GetValues(typeof(RobotinEnumRobot2)))
            {
                ButtonX buttonX1 = new ButtonX();
                buttonX1 = new DevComponents.DotNetBar.ButtonX();
                //buttonX1 = new Button();
                // 
                // buttonX1
                // 
                buttonX1.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
                buttonX1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                buttonX1.BackColor = System.Drawing.SystemColors.Control;
                //buttonX1.ColorTable = DevComponents.DotNetBar.eButtonColor.Blue;
                buttonX1.Font = new System.Drawing.Font("微软雅黑", 9.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                buttonX1.Location = new System.Drawing.Point(5, 10 + 29 * index);
                buttonX1.PopupSide = DevComponents.DotNetBar.ePopupSide.Right;
                buttonX1.Shape = new DevComponents.DotNetBar.RoundRectangleShapeDescriptor(4);
                buttonX1.Size = new System.Drawing.Size(this.p_Robot.Width - 2 * 5, 26);
                //buttonX1.Style = DevComponents.DotNetBar.eDotNetBarStyle.OfficeMobile2014;
                buttonX1.TabIndex = 0;
                buttonX1.Text = EnumHelper.GetEnumDescriptionFromValue((RobotinEnumRobot2)item);
                startChar += 0x01;
                //buttonX1.Click += Module_Click;
                //buttonX1.Image = iL_Circles.Images[i];
                buttonX1.Style = eDotNetBarStyle.StyleManagerControlled;
                buttonX1.ColorTable = eButtonColor.Flat;
                buttonX1.Tag = (RobotinEnumRobot2)item;
                //if (!string.IsNullOrEmpty(ModuleNames[i]))
                //{
                //    //buttonX1.Checked = !SystemConfig.Instance.ModuleName2Disable[ModuleNames[i]];
                //    buttonX1.BackColor = GlobalVariable.RobotStatus[i] ? Color.LimeGreen : Color.Gray;
                //    buttonX1.TextColor = SystemConfig.Instance.ModuleName2Disable[ModuleNames[i]] ? Color.White : Color.Black;
                //}
                this.p_Robot2.Controls.Add(buttonX1);
                if (index > 3)
                    break;
                index++;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Control button in this.p_Robot.Controls)
            {
                if (button is ButtonX)
                {
                    if(GlobalVariable.RobotStatus[(int)(button as ButtonX).Tag])
                    {

                    }
                    (button as ButtonX).BackColor = GlobalVariable.RobotStatus[(int)(button as ButtonX).Tag] ? Color.LimeGreen : Color.Gray;
                    //(button as ButtonX).TextColor = (Boolean)(button as ButtonX).Tag ? Color.White : Color.Black;
                }
            }
            foreach (Control button in this.p_Robot2.Controls)
            {
                if (button is ButtonX)
                {
                    (button as ButtonX).BackColor = GlobalVariable.RobotStatus2[(int)(button as ButtonX).Tag] ? Color.LimeGreen : Color.Gray;
                    //(button as ButtonX).TextColor = (Boolean)(button as ButtonX).Tag ? Color.White : Color.Black;
                }
            }
            cB_RobotEmgStop.Checked = GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人紧急停止];
            cB_RobotStop.Checked = GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人停止];
            cB_RobotStart.Checked = GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人程序启动];
            cB_RobotReset.Checked = GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人程序复位];
            cB_WarnReset.Checked = GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人报警复位];

            cB_RobotEmgStop2.Checked = GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot2.机器人紧急停止];
            cB_RobotStop2.Checked = GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot2.机器人停止];
            cB_RobotStart2.Checked = GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot2.机器人程序启动];
            cB_RobotReset2.Checked = GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot2.机器人程序复位];
            cB_WarnReset2.Checked = GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot2.机器人报警复位];
        }

        private void btn_ConveyorStart_Click(object sender, EventArgs e)
        {
            if((sender as ButtonX).Text.Equals("紧急停止"))
            {
                GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人紧急停止] = !GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人紧急停止];
            }
            else if ((sender as ButtonX).Text.Equals("停止"))
            {
                GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人停止] = !GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人停止];
            }
            else if ((sender as ButtonX).Text.Equals("程序启动"))
            {
                GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人程序启动] = !GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人程序启动];
            }
            else if ((sender as ButtonX).Text.Equals("程序复位"))
            {
                GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人程序复位] = !GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人程序复位];
            }
            else if ((sender as ButtonX).Text.Equals("报警复位"))
            {
                GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人报警复位] = !GlobalVariable.RobotControlStatus[(int)RobotoutEnumRobot1.机器人报警复位];
            }
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if ((sender as ButtonX).Text.Equals("紧急停止"))
            {
                GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人紧急停止] = !GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人紧急停止];
            }
            else if ((sender as ButtonX).Text.Equals("停止"))
            {
                GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人停止] = !GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人停止];
            }
            else if ((sender as ButtonX).Text.Equals("程序启动"))
            {
                GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人程序启动] = !GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人程序启动];
            }
            else if ((sender as ButtonX).Text.Equals("程序复位"))
            {
                GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人程序复位] = !GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人程序复位];
            }
            else if ((sender as ButtonX).Text.Equals("报警复位"))
            {
                GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人报警复位] = !GlobalVariable.RobotControlStatus2[(int)RobotoutEnumRobot1.机器人报警复位];
            }
        }
    }
}
